<template>
  <div class="main_box" style="padding:10px">
    <div class="add_wrap">
      <div class="tit">
        <h2>{{ dataForm.programId ? '编辑应用' : '新增应用' }}</h2>
        <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
      </div>
      <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
        <el-row>
          <el-form-item prop="appName" label="应用名称">
            <el-input type="text" v-model="dataForm.appName" clearable placeholder="请输入应用名称"></el-input>
          </el-form-item>
          <el-form-item prop="appType" label="应用类型">
            <el-select v-model="dataForm.appType" placeholder="请选择应用类型">
              <el-option v-for="item in programTypeList" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item prop="logo" label="应用logo">
              <UploadImg folder="program" @uploadFileList="getFileUrl">
                <img v-if="dataForm.logo" width="100%" :src="dataForm.logo" alt="">
              </UploadImg>
            </el-form-item> -->
        </el-row>
        <el-row>
          <el-form-item>
            <el-button plain type="primary" @click="sureClick">保 存</el-button>
            <el-button plain @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
  
<script>
import * as message from "@/constants/message";
import SoftwareApi from '@/api/organ/software';
import UploadImg from "@/components/UploadImg.vue";
import TenantApi from "@/api/system/tenant";
export default {
  data() {
    return {
      dataForm: {
        id: "",
        appName: '',
        appType: '',
      },
      programTypeList: [
        { id: 1, name: "呼叫器" },
        { id: 2, name: "其他" },
      ],
      tenantList: [],
      roleRules: {
        appName: [
          { required: true, message: "请输入应用名称", trigger: "focus" }
        ],
        /*appId:[
          {required:true,message:"请输入应用ID",trigger:"focus"}
        ],
        appSecret:[
          {required:true,message:"请输入应用密钥",trigger:"focus"}
        ],*/
        appType: [
          { required: true, message: "请选择应用类型", trigger: "change" }
        ],

      }
    }
  },
  components: {
    UploadImg
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (this.$route.query.id) {
        this.dataForm.id = this.$route.query.id
        this.getData()
      }
    },
    keydown(e) {
      if (e.keyCode === 32) {
        e.returnValue = false;
      }
    },
    getData() {
      SoftwareApi.detailSoftware({
        appId: this.dataForm.id
      }).then(res => {
        this.$Utils.bindingBean(this.dataForm, res.data)
      })
    },

    // getFileUrl(val){
    //   this.dataForm.logo = val[0]
    // },
    sureClick() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
            .then(() => {
              if (this.dataForm.id) {
                this.editData();
              } else {
                this.addData();
              }
            });
        }
      })
    },
    addData() {
      let param = {
        appName: this.dataForm.appName,
        appType: this.dataForm.appType,
      };
      SoftwareApi.addSoftware(param).then(res => {
        this.$Utils.notifySuccess()
        this.returnList();
      })
    },
    editData() {
      let param = {
        appId: this.dataForm.id,
        appName: this.dataForm.appName,
        appType: this.dataForm.appType,
      };
      SoftwareApi.editSoftware(param)
        .then(data => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
    },
    returnList() {
      this.$store.dispatch('delView', this.$route);
      this.$router.go(-1)
    }
  },

}
</script>
<style lang="scss" scoped>
@import "@/assets/css/views/add.scss";
</style>
  
<style lang="scss" scoped>
.upload-img {
  &::v-deep .el-upload-dragger {
    display: flex;
    justify-content: space-around;
    align-items: center;

    .tip {
      padding-bottom: 30px;
    }
  }
}
</style>
  
  